package org.distributed.transaction.core.context;

import java.sql.Connection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.distributed.transaction.core.callback.BusinessCallback;
import org.distributed.transaction.core.enums.DTTransactionType;
import org.distributed.transaction.core.enums.Propagation;

/**
 * 事务上下文
 */
public class TransactionContext {

	private static ThreadLocal<TransactionContext> transactionContextLocal = new ThreadLocal<TransactionContext>();

	/**
	 * 缓存数据库代理连接 key groupId
	 */
	private static Map<String, Connection> connectionMap = new ConcurrentHashMap<String, Connection>();

	/**
	 * 获取事务上下文
	 * 
	 * @return
	 */
	public static TransactionContext get() {
		TransactionContext transactionContext = transactionContextLocal.get();
		if (transactionContext == null) {
			transactionContext = new TransactionContext();
			transactionContextLocal.set(transactionContext);
		}
		return transactionContext;
	}

	/**
	 * 设置事务上下文
	 * 
	 * @return
	 */
	public static void set(TransactionContext transactionContext) {
		transactionContextLocal.set(transactionContext);
	}

	/**
	 * 获取数据库连接
	 * 
	 * @param groupId
	 * @return
	 */
	public Connection getConnection(String groupId) {
		return connectionMap.get(groupId);
	}

	/**
	 * 保存数据库连接
	 * 
	 * @param groupId
	 * @param connection
	 */
	public void setConnection(String groupId, Connection connection) {
		connectionMap.put(groupId, connection);
	}

	/**
	 * 事务组id
	 */
	private String groupId;

	/**
	 * 事务发起方
	 */
	private boolean transactionStart;

	/**
	 * 事务类型
	 */
	private DTTransactionType dtTransactionType;

	/**
	 * 事务信息
	 */
	private TransactionInfo transactionInfo;

	/**
	 * 业务回调方法
	 */
	private BusinessCallback businessCallback;

	/**
	 * 事务传播机制
	 */
	private Propagation Propagation;
	
	/**
	 * 事务状态
	 */
	private TransactionStatus transactionStatus;

	public TransactionInfo getTransactionInfo() {
		return transactionInfo;
	}

	public void setTransactionInfo(TransactionInfo transactionInfo) {
		this.transactionInfo = transactionInfo;
	}

	public BusinessCallback getBusinessCallback() {
		return businessCallback;
	}

	public void setBusinessCallback(BusinessCallback businessCallback) {
		this.businessCallback = businessCallback;
	}

	public Propagation getPropagation() {
		return Propagation;
	}

	public void setPropagation(Propagation propagation) {
		Propagation = propagation;
	}

	public boolean isTransactionStart() {
		return transactionStart;
	}

	public void setTransactionStart(boolean transactionStart) {
		this.transactionStart = transactionStart;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public DTTransactionType getDtTransactionType() {
		return dtTransactionType;
	}

	public void setDtTransactionType(DTTransactionType dtTransactionType) {
		this.dtTransactionType = dtTransactionType;
	}

	public TransactionStatus getTransactionStatus() {
		return transactionStatus;
	}

	public void setTransactionStatus(TransactionStatus transactionStatus) {
		this.transactionStatus = transactionStatus;
	}

}
